﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Solidcylindricalreamer
    {
        public static void _Solidcylindricalreamer(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 2; //放大系数

            double oal = 47.96;
            double l = 22.63;
            double dconms = 4.88;
            double lf = 46.22;
            double dn = 3.94;
            double dc = 6.06;
            double ls = 13.37;
            double plgang = 34;
            double lpr = 37.44;



            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dconms / 2 - 0.3);
            Vector2 v5 = new Vector2(v4.X + 0.3, v4.Y + 0.3);
            Vector2 v6 = new Vector2(v3.X + ls, v5.Y);
            Vector2 v7 = new Vector2(v6.X + 0.5, v3.Y + dn / 2);
            Vector2 v8 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v9 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v10 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v11 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v12 = new Vector2(v3.X + lf, v3.Y + dc / 2);
            Vector2 v13 = new Vector2(v3.X + oal, v12.Y - (oal - lf) * Math.Tan(plgang * pi / 180));
            Vector2 v14 = new Vector2(v13.X, v13.Y * -1);
            Vector2 v15 = new Vector2(v12.X, v12.Y * -1);
            Vector2 v16 = new Vector2(v13.X - l, v12.Y);
            Vector2 v17 = new Vector2(v16.X - 0.8, v7.Y);
            Vector2 v18 = new Vector2(v16.X, v16.Y * -1);
            Vector2 v19 = new Vector2(v17.X, v17.Y * -1);
            Vector2 v20 = new Vector2(v13.X, v14.Y + dc / 25.25);
            Vector2 v21 = new Vector2(v16.X, v20.Y);
            Vector2 v22 = new Vector2(v13.X - lpr, v3.Y);
            Vector2 v23 = new Vector2(v22.X, v22.Y + dconms / 2 + 1);
            Vector2 v24 = new Vector2(v23.X, v23.Y * -1);
            Vector2 a1 = new Vector2(v13.X, v3.Y);

            Vector2 v25 = new Vector2(v2.X + oal / 2.887, v2.Y);
            Vector2 v26 = new Vector2(v25.X + oal / 16.203 + 2, v25.Y);
            Vector2 v27 = new Vector2(v25.X + 1, v25.Y);
            Vector2 v28 = new Vector2(v27.X, v27.Y + dc / 2.97);
            Vector2 v29 = new Vector2(v27.X, v27.Y + dc / 2);
            Vector2 v32 = new Vector2(v29.X + oal / 16.203, v29.Y);
            Vector2 v30 = new Vector2(v29.X + (v32.X - v29.X) / 3, v29.Y - dc / 46.615);
            Vector2 v31 = new Vector2(v30.X + (v32.X - v29.X) / 3, v30.Y);
            Vector2 v33 = new Vector2(v32.X, v32.Y * -1);
            Vector2 v34 = new Vector2(v31.X, v31.Y * -1);
            Vector2 v35 = new Vector2(v30.X, v30.Y * -1);
            Vector2 v36 = new Vector2(v29.X, v29.Y * -1);
            Vector2 v37 = new Vector2(v28.X, v28.Y * -1);
            Vector2 v38 = new Vector2(v32.X, v37.Y);
            Vector2 v39 = new Vector2(v38.X, v28.Y);



            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            fun.PolyFill(dxf, line2, 0.1, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v7));
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v9));
            line3.Vertexes.Add(new LwPolylineVertex(v6));
            line3.Vertexes.Add(new LwPolylineVertex(v7));
            line3.Vertexes.Add(new LwPolylineVertex(v17));
            line3.Vertexes.Add(new LwPolylineVertex(v19));
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            fun.PolyFill(dxf, line3, 0.1, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v16));
            line4.Vertexes.Add(new LwPolylineVertex(v18));
            line4.Vertexes.Add(new LwPolylineVertex(v19));
            line4.Vertexes.Add(new LwPolylineVertex(v17));
            line4.Vertexes.Add(new LwPolylineVertex(v16));
            line4.Vertexes.Add(new LwPolylineVertex(v12));
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes.Add(new LwPolylineVertex(v14));
            line4.Vertexes.Add(new LwPolylineVertex(v15));
            line4.Vertexes.Add(new LwPolylineVertex(v18));
            fun.PolyFill(dxf, line4, 0.1, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v20));
            line5.Vertexes.Add(new LwPolylineVertex(v21));
            fun.PolyFill(dxf, line5, 0.1, contour);

            Line line6 = new Line(v23, v24);
            line6.Layer = centre;
            line6.Linetype = Linetype.DashDot;
            line6.LinetypeScale = 3;
            dxf.AddEntity(line6);

            //2
            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v29));
            line7.Vertexes.Add(new LwPolylineVertex(v30));
            line7.Vertexes.Add(new LwPolylineVertex(v31));
            line7.Vertexes.Add(new LwPolylineVertex(v32));
            line7.Vertexes.Add(new LwPolylineVertex(v33));
            line7.Vertexes.Add(new LwPolylineVertex(v34));
            line7.Vertexes.Add(new LwPolylineVertex(v35));
            line7.Vertexes.Add(new LwPolylineVertex(v36));
            line7.Vertexes.Add(new LwPolylineVertex(v29));
            line7.Vertexes[0].Bulge = Math.Tan(80 * pi / 180 / 4);
            line7.Vertexes[1].Bulge = Math.Tan(-62 * pi / 180 / 4);
            line7.Vertexes[2].Bulge = Math.Tan(59 * pi / 180 / 4);
            line7.Vertexes[4].Bulge = Math.Tan(59 * pi / 180 / 4);
            line7.Vertexes[5].Bulge = Math.Tan(-62 * pi / 180 / 4);
            line7.Vertexes[6].Bulge = Math.Tan(80 * pi / 180 / 4);
            fun.PolyFill(dxf, line7, 0.1, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v28));
            line8.Vertexes.Add(new LwPolylineVertex(v39));
            line8.Vertexes.Add(new LwPolylineVertex(v38));
            line8.Vertexes.Add(new LwPolylineVertex(v37));
            fun.PolyFill(dxf, line8, 0.1, contour);

            Line line9 = new Line(v25, v26);
            line9.Layer = centre;
            line9.Linetype = Linetype.DashDot;
            line9.LinetypeScale = 3;
            dxf.AddEntity(line9);


            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v29));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v30));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v31));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v32));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v39));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v29));
            hatchpoly1.Vertexes[0].Bulge = Math.Tan(80 * pi / 180 / 4);
            hatchpoly1.Vertexes[1].Bulge = Math.Tan(-62 * pi / 180 / 4);
            hatchpoly1.Vertexes[2].Bulge = Math.Tan(59 * pi / 180 / 4);
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);

            LwPolyline hatchpoly2 = new LwPolyline();
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v36));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v35));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v34));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v33));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v38));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v37));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v36));
            hatchpoly2.Vertexes[0].Bulge = Math.Tan(-80 * pi / 180 / 4);
            hatchpoly2.Vertexes[1].Bulge = Math.Tan(62 * pi / 180 / 4);
            hatchpoly2.Vertexes[2].Bulge = Math.Tan(-59 * pi / 180 / 4);
            hatchpoly2.IsClosed = true;
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            Arc[] harc2 = new Arc[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多
                                                       //
                                                       //少
            for (int i = 0; i < hnumst2; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
                if (htype2[i] == "Arc")
                {
                    harc2[i] = (Arc)hatchpoly2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype2, hnumst2, 45, 10, hline2, harc2, hatching);

            //坐标系 
            fun.Axis(dxf, v3, -90, 180, "", "", "", 0.3, 1, 0);

            fun.Axis(dxf, a1, -90, 180, "", "", "", 0.3, 1, 0);

            fun.Axis(dxf, v22, -90, 180, "", "", "", 0.3, 1, 0);


            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;

            fun.dimrot1(dxf, v4, v13, 0, oal / 2, 1, height, arrowsize, exline, "OAL", "", "");

            fun.dimrot1(dxf, v23, v13, 0, oal / 2.5, 1, height, arrowsize, exline, "LPR", "", "");

            fun.dimrot1(dxf, new Vector2(v7.X + 3, v7.Y), v13, 0, oal / 3, 1, height, arrowsize, exline, "LU", "", "");

            fun.dimrot1(dxf, v16, v13, 0, oal / 4, 1, height, arrowsize, exline, "L", "", "");

            fun.dimrot1(dxf, v12, v15, -90, dc * 2, 1, height, arrowsize, exline, "DC", "", "");

            fun.dimrot1(dxf, v13, v14, -90, dc, 1, height, arrowsize, exline, "DCINTF", "", "");

            fun.dimrot1(dxf, v7, v8, -90, dc, 1, height, arrowsize, exline, "DN", "", "");

            fun.dimrot1(dxf, v5, v10, 90, dconms , 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v11, v9, 180, oal / 3, 1, height, arrowsize, exline, "LS", "", "");

            fun.dimrot1(dxf, v14, v15, 180, oal / 3, 1, height, arrowsize, exline, "PLGL", "", "");

            fun.dimrot1(dxf, v24, v15, 180, oal / 2.5, 1, height, arrowsize, exline, "LTA", "", "");

            fun.dimrot1(dxf, v11, v15, 180, oal / 2, 1, height, arrowsize, exline, "LF", "", "");

            fun.jdbz2(dxf, new Line(v12, fun.Polar(v12, (180 - plgang) * pi / 180, 0.1)), new Line(a1, fun.Polar(a1, 180 * pi / 180, 0.1)), dc * 2, height, arrowsize, exline, "PLGANG", "", "");

            //2
            fun.dimrot1(dxf, v28, v37, 90, dc, 1, height, arrowsize, exline, "PHD", "", "");


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}